import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter_navigator/Core/OSState.dart';
import 'package:flutter_navigator/OSNavigator/OSNavigator.dart';

import 'Model/VideoModel.dart';

/// 创建全局共享的event bus
final eventBus = EventBus();

/// 创建自己需要共享的数据
class AMCommonViewModel {
  int count;

  AMCommonViewModel(this.count);
}

class AMHomePage extends StatefulWidget {
  const AMHomePage({Key? key}) : super(key: key);
  @override
  _AMHomePageState createState() => _AMHomePageState();
}

class _AMHomePageState extends OSState<AMHomePage> {
  var listener;
  var model = AMCommonViewModel(15);

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    OSNavigator.getInstance().addListener(this.listener = (current, pre) {
      print("当前页面：${current.page}");
      print("上一个页面：${pre.page}");
    });
  }

  @override
  void dispose() {
    // TODO: implement dispose
    OSNavigator.getInstance().removeListener(this.listener);
    super.dispose();
  }

  // 变量声明在 build方法外面，如果在build内部声明，重新build每次都会声明，相当于变量永远是新的，值永远是初始化的值
  Widget build(BuildContext context) {
    /// 2.在程序的顶层创建ChangeNotifierProvider(我这里仅限HomePage,如果需要最顶层，可以包裹runApp)
    return Scaffold(
      appBar: AppBar(
        title: Text("逻辑资源"),
      ),
      body: Column(
        children: [
          Container(
            height: 110,
            width: double.infinity,
            child: Card(
              color: Colors.orange,
              child: Column(children: [
                MaterialButton(
                  color: Colors.redAccent,
                  textColor: Colors.white,
                  onPressed: () {
                    OSNavigator.getInstance().onJumpTo(RouteStatus.homeDetail,
                        args: {"videoModel": VideoModel(180)});
                  },
                  child: Text("点击跳转详情"),
                ),
                MaterialButton(
                    color: Colors.redAccent,
                    textColor: Colors.white,
                    onPressed: () {
                      /// 修改数据
                      setState(() {
                        model.count += 1;
                        eventBus.fire(model);
                      });
                    },
                    child: Text("计数器+1"))
              ]),
            ),
          ),
          AMCardView01(),
          AMCardView02()
        ],
      ),
    );
  }
}

class AMCardView01 extends StatefulWidget {
  const AMCardView01({Key? key}) : super(key: key);

  @override
  _AMCardView01State createState() => _AMCardView01State();
}

class _AMCardView01State extends State<AMCardView01> {
  int count = 10;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    eventBus.on<AMCommonViewModel>().listen((commonModel) {
      count = commonModel.count;
      print("count:${commonModel.count}");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 80,
      width: double.infinity,
      child: Card(
        color: Colors.orange,
        child: Column(
          children: [
            Center(
              child: Text("当前的数量为$count"),
            )
          ],
        ),
      ),
    );
  }
}

class AMCardView02 extends StatefulWidget {
  ///3.在其他位置共享数据
  const AMCardView02({Key? key}) : super(key: key);

  @override
  _AMCardView02State createState() => _AMCardView02State();
}

class _AMCardView02State extends State<AMCardView02> {
  int count = 10;

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

    eventBus.on<AMCommonViewModel>().listen((commonModel) {
      count = commonModel.count;
      print("count:${commonModel.count}");
    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      height: 80,
      width: double.infinity,
      child: Card(
        color: Colors.yellow,
        child: Column(
          children: [
            Center(
              child: Text("当前的数量为$count"),
            )
          ],
        ),
      ),
    );
  }
}
